<?php
/*
 * @Created by: wangchengcheng
 * @Author: wangchengcheng
 * @Date: 2020-01-07 10:53:07
 * @LastEditors  : wangchengcheng
 * @LastEditTime : 2020-01-09 10:27:32
 */

namespace App\Observers;

use App\Models\Article;
use App\Models\ArticleTag;
use Artisan;

class TagObserver extends BaseObserver
{
    public function created($model)
    {
        parent::created($model);

        Artisan::queue('wblog:generateSitemap');
    }

    public function saving($tag)
    {
        if($tag->isDirty('name') && empty($tag->slug)){
            $tag->slug = generate_english_slug($tag->name);
        }
    }

    public function deleting($tag)
    {
        if(ArticleTag::where('tag_id', $tag->id)->count() !== 0){
            flash_error('此标签下有文章,不可以删除.');
            return false;
        }
    }
    
    public function deleted($tag)
    {
        parent::deleted($tag);

        if(!$tag->isForceDeleting()){
            Artisan::queue('wblog:generateSitemap');
        }
    }
}
